package it.unisalento.appsalento.activity.fragment;

import it.unisalento.R;
import it.unisalento.appsalento.constants.Constants;
import it.unisalento.appsalento.activity.App;
import it.unisalento.appsalento.adapter.ListaEsamiAdapter;
import it.unisalento.appsalento.model.Esame;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
//import it.unisalento.appsalento.adapter.EsamiAdapter;

/**
 * Fragment che contiene la lista degli esami sostenuti e da sostenere
 */
public class LibrettoFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View rootView = inflater.inflate(R.layout.fragment_libretto, container,
				false);
		setHasOptionsMenu(true);
		ListView listView = (ListView) rootView.findViewById(R.id.list_view_libretto);

		ListaEsamiAdapter adapter = new ListaEsamiAdapter(getActivity(), R.layout.row_libretto,  App.listaEsami);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				if (App.dettaglioEsameFragment == null) {
					App.dettaglioEsameFragment = new DettaglioEsameFragment();
				}
				
				Esame esame = (Esame) parent.getItemAtPosition(position);
				
				if (esame==null) {
					// TODO: gestire la casistica di errore
					return;
				}
				
				App.esameCorrente = esame;

				FragmentManager fragmentManager = getFragmentManager();
				fragmentManager
						.beginTransaction()
						.replace(R.id.content_frame, App.dettaglioEsameFragment)
						.addToBackStack(null)
						.commit();
				
				Intent intent = new Intent(Constants.ESAME_DETTAGLIO_EVENT);
				intent.putExtra(Constants.ESAME_COD_EXTRA_NAME, esame.getCodice());
				LocalBroadcastManager.getInstance(
						getActivity().getApplicationContext()).sendBroadcast(
						intent);
			}
		});
		
		SharedPreferences pref = getActivity().getSharedPreferences(
				Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS);   
		String guideAlreadyShown = pref.getString(Constants.PREF_GUIDE_ALREADY_SHOWN, null);
		
		//Se la legenda non e' ancora mai stata mostrata, la mostriamo
		if (guideAlreadyShown == null || guideAlreadyShown.equals("")) {
			getActivity().getFragmentManager().beginTransaction()
				.add(R.id.content_frame, new LibrettoGuidaFragment())
				.addToBackStack(null)
				.commit();
			/* Stiamo mostrando la legenda per la prima volta. Aggiungiamo la
			 * chiave nelle shared preferences per non mostrarla piu'
			 */
			getActivity().getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS)
				.edit()
				.putString(Constants.PREF_GUIDE_ALREADY_SHOWN, Constants.PREF_GUIDE_ALREADY_SHOWN)
				.commit();
		}
		
		return rootView;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.libretto, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.action_legenda:
				getActivity().getFragmentManager().beginTransaction()
				.add(R.id.content_frame, new LibrettoGuidaFragment())
				.addToBackStack(null)
				.commit();
			default:
				return super.onOptionsItemSelected(item);
		}
	}

}
